package com.vancouver.knows.ui.fragment.citynews;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.bumptech.glide.Glide;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.MainApplication.TrackerName;
import com.vancouver.knows.business.BUtils;
import com.vancouver.knows.business.bean.BannerInfo;
import com.vancouver.knows.ui.activity.WebViewActivity;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.activity.event.EventDetailActivity;
import com.vancouver.knows.ui.adapters.RelatedNewsAdapter;
import com.vancouver.knows.ui.view.AutoGridLinearlayout;
import com.vancouver.knows.ui.view.RichWebView;
import com.vancouver.knows.util.AnalyticsConstants;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.CityNewsDetailInfo;

public class CityNewsPostMainDetailView {

	@Bind(R.id.title)
	TextView title;
	@Bind(R.id.upvote)
	TextView upvote;
	@Bind(R.id.sourceName)
	TextView sourceName;
	@Bind(R.id.sourceAuthor)
	TextView sourceAuthor;
	@Bind(R.id.pdg)
	TextView pdg;
	@Bind(R.id.citynewsSource)
	TextView citynewsSource;
	@Bind(R.id.content)
	RichWebView content;
	@Bind(R.id.hotCommentLayout)
	LinearLayout hotCommentLayout;
	@Bind(R.id.relatedNewsLayout)
	LinearLayout relatedNewsLayout;
	@Bind(R.id.autoList)
	AutoGridLinearlayout autoList;
	@Bind(R.id.bannerContainer)
	LinearLayout bannerContainer;
	@Bind(R.id.banner)
	ImageView banner;

	private CityNewsDetailActivity activity;
	private CityNewsPullDetailControllerView rootView;
	private CityNewsDetailInfo cityNewsDetailInfo;
	private CommentItemView commentItemView;

	public CityNewsPostMainDetailView(CityNewsDetailActivity activity) {
		this.activity = activity;
		this.cityNewsDetailInfo = activity.getInfo();
		rootView = new CityNewsPullDetailControllerView(activity);
		rootView.init(cityNewsDetailInfo.getId(), activity);
		ButterKnife.bind(this, rootView);
		init();
	}

	private void init() {
		if (BUtils.isCollectionEmpty(cityNewsDetailInfo.getCommentInfoList())) {
			hotCommentLayout.setVisibility(View.GONE);
		} else {
			hotCommentLayout.setVisibility(View.VISIBLE);
		}

		if(BUtils.isCollectionEmpty(cityNewsDetailInfo.getRelatedPostInfoList())){
			relatedNewsLayout.setVisibility(View.GONE);
		}else{
			relatedNewsLayout.setVisibility(View.VISIBLE);
			autoList.setAutoGridAdapter(new RelatedNewsAdapter(activity,1,cityNewsDetailInfo.getRelatedPostInfoList()));
		}

		title.setText(cityNewsDetailInfo.getTitle());
		updateUpvoteLayout();
		if (!StringUtil.isEmptyString(cityNewsDetailInfo.getSourceName())) {
			sourceName.setVisibility(View.VISIBLE);
			SpannableStringBuilder sourceAuthorStr = AppUtils.replaceColor(
					activity.getString(R.string.source_author, cityNewsDetailInfo.getSourceName()),
					cityNewsDetailInfo.getSourceName(), activity.getResources().getColor(R.color.orange));
			sourceName.setText(sourceAuthorStr);
			
			if(!StringUtil.isEmptyString(cityNewsDetailInfo.getSourceAuthor())){
				sourceAuthor.setVisibility(View.VISIBLE);
				sourceAuthor.setText(AppUtils.replaceColor(
						activity.getString(R.string.source_upload, cityNewsDetailInfo.getSourceAuthor()),
						cityNewsDetailInfo.getSourceAuthor(), activity.getResources().getColor(R.color.orange)));
			}else{
				sourceAuthor.setVisibility(View.GONE);
			}
			
		} else {
			sourceName.setVisibility(View.VISIBLE);
			String source = activity.getResources().getString(R.string.vgh_know);
			sourceName.setText(activity.getString(R.string.source_author, source));
			
			sourceAuthor.setVisibility(View.VISIBLE);
			sourceAuthor.setText(AppUtils.replaceColor(
					activity.getString(R.string.upload_author, cityNewsDetailInfo.getPostAuthor()),
					cityNewsDetailInfo.getPostAuthor(), activity.getResources().getColor(R.color.orange)));
		}

		pdg.setText(AppUtils.getDateStr(activity, cityNewsDetailInfo.getPdg()));

		String contentStr = cityNewsDetailInfo.getContent();
		content.loadHtmlText(contentStr);

		//展示广告
		BannerInfo bannerInfo = cityNewsDetailInfo.getAdBanner();
		if (bannerInfo != null) {
			final String imageUrl = bannerInfo.getImage();
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					bannerContainer.setVisibility(View.VISIBLE);
					Glide.with(activity).load(imageUrl).placeholder(R.drawable.no_image).crossFade().into(banner);
				}
			}, 300);

		}

		//热门评论
		commentItemView = new CommentItemView();
		commentItemView.init(activity, hotCommentLayout, cityNewsDetailInfo.getCommentInfoList(), cityNewsDetailInfo.getId());
	}

//	public Handler handler = new Handler() {
//
//		@Override
//		public void handleMessage(Message msg) {
//			super.handleMessage(msg);
//			BannerInfo bannerInfo = cityNewsDetailInfo.getAdBanner();
//			String imageUrl = bannerInfo.getImage();
//			if (bannerInfo != null) {
//				bannerContainer.setVisibility(View.VISIBLE);
//				Glide.with(activity)
//						.load(imageUrl)
//						.placeholder(R.drawable.no_image)
//						.crossFade()
//						.into(banner);
//			}
//
//		}
//	};

	public CityNewsPullDetailControllerView getRootView() {
		return rootView;
	}

	@OnClick(R.id.bannerContainer)
	public void onBannerClick() {

		BannerInfo bannerInfo = cityNewsDetailInfo.getAdBanner();
		if (bannerInfo == null) {
			return;
		}

		if (BannerInfo.TYPE_WEB.equals(bannerInfo.getType())
				&& !StringUtil.isEmptyString(bannerInfo.getUrl())) {
			Intent webIntent =  new Intent(activity, WebViewActivity.class);
			webIntent.putExtra("url", bannerInfo.getUrl());
			activity.startActivity(webIntent);
		} else if (BannerInfo.TYPE_APPLICATION.equals(bannerInfo.getType())
				&& !StringUtil.isEmptyString(bannerInfo.getPostId())) {
			inAppJumpToDetail(bannerInfo);
		}
	}

	@OnClick(R.id.citynewsSource)
	public void onCitynewsSourceClick() {
		AppUtils.showBrower(activity, cityNewsDetailInfo.getSourceUrl());
	}

	@OnClick(R.id.upvote)
	public void onUpvoteClick() {
		activity.upvote();

		Tracker t = ((MainApplication) activity.getApplication()).getTracker(TrackerName.APP_TRACKER);
		t.setScreenName(AnalyticsConstants.CUSTOM_EVENT_VALUE_CITYNEWS);
		t.send(new HitBuilders.EventBuilder().setCategory(AnalyticsConstants.CATEGORY_BTN)
				.setAction(AnalyticsConstants.ACTION_ONCLICK).setLabel(AnalyticsConstants.LABEL_CITYNEWS_DETAIL_UPVOTE)
				.build());
	}

	public void subHandleMessage(Message msg) {
		switch (msg.what) {
		case Constants.BUSINESS_STATUS_USER_COMMENT_UPVOTE:
		case Constants.BUSINESS_STATUS_USER_DELETE_COMMENT_UPVOTE:
			commentItemView.updateCommentLayout();
			break;
		}
	}

	public void taskMarkRefreshOver(ATaskMark taskMark) {
		if (taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
			rootView.refreshFinish(ARefreshViewController.SUCCEED);
		} else {
			rootView.refreshFinish(ARefreshViewController.FAIL);
		}
		cityNewsDetailInfo = activity.getInfo();
		init();
	}

	public void updateUpvoteLayout() {
		upvote.setText(String.valueOf(cityNewsDetailInfo.getUpvoteCount()));
		if ("Y".equals(cityNewsDetailInfo.getUserUpvoted())) {
			Drawable drawable = activity.getResources().getDrawable(R.drawable.icon_like_active);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			upvote.setCompoundDrawables(null, drawable, null, null);
			upvote.setTextColor(activity.getResources().getColor(R.color.orange));
		} else {
			Drawable drawable = activity.getResources().getDrawable(R.drawable.icon_like);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			upvote.setCompoundDrawables(null, drawable, null, null);
			upvote.setTextColor(activity.getResources().getColor(R.color.common_font));
		}
	}

	/**
	 * 应用内跳转
	 * @param info
	 */
	private void inAppJumpToDetail(BannerInfo info){
		if (info != null) {
			Class cla = null;
			String postType = info.getPostType();
			if ("post".equals(postType)) {
				cla = CityNewsDetailActivity.class;
			} else if ("event".equals(postType)) {
				cla = EventDetailActivity.class;
			} else if ("business".equals(postType)) {
				cla = CityDiscountDetailActivity.class;
			}
			if (cla != null) {
				Intent intent = new Intent(activity, cla);
				intent.putExtra("postId", info.getPostId());
				activity.startActivity(intent);
			}
		}
	}

}
